package com.ruoyi.lxy.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.lxy.domain.bo.TPrivateMessageBo;
import com.ruoyi.lxy.domain.vo.TPrivateMessageVo;
import com.ruoyi.lxy.domain.TPrivateMessage;
import com.ruoyi.lxy.mapper.TPrivateMessageMapper;
import com.ruoyi.lxy.service.ITPrivateMessageService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 个人信息Service业务层处理
 *
 * @author ruoyi
 * @date 2023-08-24
 */
@RequiredArgsConstructor
@Service
public class TPrivateMessageServiceImpl implements ITPrivateMessageService {

    private final TPrivateMessageMapper baseMapper;

    /**
     * 查询个人信息
     */
    @Override
    public TPrivateMessageVo queryById(Long id){
        return baseMapper.selectVoById(id);
    }

    /**
     * 查询个人信息列表
     */
    @Override
    public TableDataInfo<TPrivateMessageVo> queryPageList(TPrivateMessageBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<TPrivateMessage> lqw = buildQueryWrapper(bo);
        Page<TPrivateMessageVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询个人信息列表
     */
    @Override
    public List<TPrivateMessageVo> queryList(TPrivateMessageBo bo) {
        LambdaQueryWrapper<TPrivateMessage> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<TPrivateMessage> buildQueryWrapper(TPrivateMessageBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TPrivateMessage> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getSenderType() != null, TPrivateMessage::getSenderType, bo.getSenderType());
        lqw.eq(bo.getSender() != null, TPrivateMessage::getSender, bo.getSender());
        lqw.eq(bo.getReceiverType() != null, TPrivateMessage::getReceiverType, bo.getReceiverType());
        lqw.eq(bo.getReceiver() != null, TPrivateMessage::getReceiver, bo.getReceiver());
        lqw.eq(bo.getIsRead() != null, TPrivateMessage::getIsRead, bo.getIsRead());
        return lqw;
    }

    /**
     * 新增个人信息
     */
    @Override
    public Boolean insertByBo(TPrivateMessageBo bo) {
        TPrivateMessage add = BeanUtil.toBean(bo, TPrivateMessage.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setId(add.getId());
        }
        return flag;
    }

    /**
     * 修改个人信息
     */
    @Override
    public Boolean updateByBo(TPrivateMessageBo bo) {
        TPrivateMessage update = BeanUtil.toBean(bo, TPrivateMessage.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(TPrivateMessage entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除个人信息
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
